package core.io;

import core.server.HttpServlet;
import lombok.AllArgsConstructor;

import java.net.Socket;
import java.util.Map;

/**
 * @author wsz
 * @date 2021/11/16 15:21
 **/
@AllArgsConstructor
public class RequestProcessor extends Thread {

    private String appBase;
    private Socket socket;
    private Map<String, HttpServlet> servletMap;

    @Override
    public void run() {
        try {
            Request request = new Request(socket.getInputStream(), appBase);
            Response response = new Response(socket.getOutputStream());

            HttpServlet servlet = servletMap.get(request.getUrl());
            if (servlet == null) {
                response.outputHtml(request);
            } else {
                servlet.service(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
